package com.scs.application.modules.sys.sync.impl;

import com.scs.application.core.utils.BeanUtils;
import com.scs.application.modules.sys.adapter.FlowAdapter;
import com.scs.application.modules.sys.dto.FlowDTO;
import com.scs.application.modules.sys.entity.Flow;
import com.scs.application.modules.sys.entity.FlowButton;
import com.scs.application.modules.sys.entity.FlowNode;
import com.scs.application.modules.sys.properties.SystemProperties;
import com.scs.application.modules.sys.service.FlowButtonService;
import com.scs.application.modules.sys.service.FlowNodeService;
import com.scs.application.modules.sys.service.FlowService;
import com.scs.application.modules.sys.sync.FlowSyncService;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Description:流程同步实现类
 */
@Service
public class FlowSyncServiceImpl implements FlowSyncService {

    @Autowired
    private SystemProperties systemProperties;

    @Autowired
    private FlowAdapter flowAdapter;

    /**
     * 同步流程数据
     */
    @Override
    public void syncFlow() {
        if (systemProperties.getFlagProject()) { //如果是项目的，从产品进行同步
            FlowService fileFlowService = flowAdapter.getFlowService(false);
            List<FlowDTO> fileFlowDTOs = fileFlowService.getDTOList();
            if (fileFlowDTOs == null || fileFlowDTOs.isEmpty()) {
                return;
            }

            List<Flow> needUpdateFlows = Lists.newArrayList();
            List<FlowButton> needUpdateFlowButtons = Lists.newArrayList();
            List<FlowNode> needUpdateFlowNodes = Lists.newArrayList();
            List<Flow> flows = flowAdapter.getFlowService().list();
            for (FlowDTO flowDTO : fileFlowDTOs) {
                if (!flows.stream().filter(flow -> flowDTO.getCode().equalsIgnoreCase(flow.getCode())).findAny().isPresent()) {
                    Flow flow = new Flow();
                    BeanUtils.copyProperties(flowDTO, flow);
                    needUpdateFlows.add(flow);

                    if (flowDTO.getFlowNodes() != null && !flowDTO.getFlowNodes().isEmpty()) {
                        flowDTO.getFlowNodes().forEach(flowNodeDTO -> {
                            FlowNode flowNode = new FlowNode();
                            BeanUtils.copyProperties(flowNodeDTO, flowNode);
                            flowNode.setFlowId(flow.getId());
                            needUpdateFlowNodes.add(flowNode);
                        });
                    }

                    if (flowDTO.getFlowButtons() != null && !flowDTO.getFlowButtons().isEmpty()) {
                        flowDTO.getFlowButtons().forEach(flowButtonDTO -> {
                            FlowButton flowButton = new FlowButton();
                            BeanUtils.copyProperties(flowButtonDTO, flowButton);
                            flowButton.setFlowId(flow.getId());
                            needUpdateFlowButtons.add(flowButton);
                        });
                    }
                }
            }
            if (needUpdateFlows.size() > 0) {
                flowAdapter.getFlowService().saveOrUpdateBatch(needUpdateFlows);
            }
            if (needUpdateFlowNodes.size() > 0) {
                flowAdapter.getFlowNodeService().saveOrUpdateBatch(needUpdateFlowNodes);
            }
            if (needUpdateFlowButtons.size() > 0) {
                flowAdapter.getFlowButtonService().saveOrUpdateBatch(needUpdateFlowButtons);
            }
        } else { //如果是产品的，第一次没有任何数据，从表里获取
            List<Flow> fileFlows = flowAdapter.getFlowService().list();
            if (fileFlows == null || fileFlows.isEmpty()) {
                FlowService flowService = flowAdapter.getFlowService(true);
                List<Flow> flows = flowService.list();
                for (Flow flow : flows) {
                    flowAdapter.getFlowService().saveOrUpdate(flow);
                }

                FlowButtonService flowButtonService = flowAdapter.getFlowButtonService(true);
                List<FlowButton> flowButtons = flowButtonService.list();
                for (FlowButton flowButton : flowButtons) {
                    flowAdapter.getFlowButtonService().saveOrUpdate(flowButton);
                }

                FlowNodeService flowNodeService = flowAdapter.getFlowNodeService(true);
                List<FlowNode> flowNodes = flowNodeService.list();
                for (FlowNode flowNode : flowNodes) {
                    flowAdapter.getFlowNodeService().saveOrUpdate(flowNode);
                }
            }
        }
    }
}
